/*
 * 
 */
package fr.univtln.superplanning.gui.login;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import fr.univtln.superplanning.controller.DBLoginController;
import fr.univtln.superplanning.gui.LayoutConfiguration;
import fr.univtln.superplanning.persistence.DBLogin;
import fr.univtln.superplanning.persistence.DatabaseManager;

// TODO: Auto-generated Javadoc
/**
 * This class provides a basic GUI to login. <br>
 * It class use MVC. <br>
 * It's control a DBLogin instance with a DBLoginController.
 * 
 * @author vincent
 */
public class DBLoginView extends JDialog implements Observer {

	/** The images names. */
	public static final String[] IMAGES_NAMES = { "1.png", "2.png", "3.png", "4.png", "5.png",
			"6.png" };

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The controller. */
	private DBLoginController controller;

	/** The drivers combo box. */
	private JComboBox driversComboBox = new JComboBox();

	/** The host text field. */
	private JTextField hostTextField = new JTextField();

	/** The port text field. */
	private JTextField portTextField = new JTextField();

	/** The user name text field. */
	private JTextField userNameTextField = new JTextField();

	/** The password field. */
	private JPasswordField passwordField = new JPasswordField();

	/** The login button. */
	private JButton loginButton = new JButton("Connexion");

	/** The cancel button. */
	private JButton cancelButton = new JButton("Annuler");

	/**
	 * Instantiates a new dB login view.
	 * 
	 * @param controller
	 *            the controller
	 */
	public DBLoginView(DBLoginController controller) {

		this.controller = controller;

		controller.getDbLogin().addObserver(this);
		// now every time the DBLogin change, the update methode is called

		initComponents();
	}

	/**
	 * Initialise the components.
	 */
	public void initComponents() {

		// Widgets configuration

		// For each driver name, display it !
		for (Entry<String, String> driverName : DatabaseManager.DATABASE_DRIVERS.entrySet())
			driversComboBox.addItem(driverName.getKey());

		userNameTextField.setToolTipText("Entrez ici votre login");
		passwordField.setToolTipText("Entrez ici votre mot de passe");
		hostTextField.setToolTipText("Nom d'hôte ou adresse IP");
		portTextField.setToolTipText("Port de connexion à la base de données");

		setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 1;
		getContentPane().add(createPresentationPanel(), constraints);
		constraints.gridy = 1;
		getContentPane().add(createConfigurationPanel(), constraints);
		constraints.gridy = 2;
		getContentPane().add(createLoginPanel(), constraints);

		// Listeners...
		loginButton.addActionListener(controller); // delegate the action to the
													// controller.
		cancelButton.addActionListener(controller);

		getRootPane().setDefaultButton(loginButton);

		// Window configuration...
		setTitle("Superplanning - Connexion");

		Dimension screenDimension = getToolkit().getScreenSize();
		pack();
		// configure a minimum size for the frame.
		setMinimumSize(getSize());
		// place the frame on the center of the window.
		setLocation(screenDimension.width / 2 - getWidth() / 2, screenDimension.height / 2
				- getHeight() / 2);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	/**
	 * This method creates the presentation panel with the title and random
	 * image.
	 * 
	 * @return the component created.
	 */
	public JComponent createPresentationPanel() {

		JPanel presentationPanel = new JPanel();
		presentationPanel.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 1;
		constraints.gridwidth = 3;
		constraints.insets = LayoutConfiguration.INSETS;
		JLabel titleLabel = new JLabel("Superplanning - Connexion");
		titleLabel.setFont(new Font("Courier", Font.ITALIC, 20));
		presentationPanel.add(titleLabel, constraints);

		constraints.gridy = 1;
		URL urlImage = getClass().getResource(
				"/resources/images/"
						+ IMAGES_NAMES[1 + (int) (Math.random() * (IMAGES_NAMES.length - 1))]);
		if (urlImage != null) {
			JLabel imageLabel = new JLabel(new ImageIcon(urlImage));
			presentationPanel.add(imageLabel, constraints);
		}

		return presentationPanel;

	}

	/**
	 * This method creates the configuration panel for login into the database.
	 * 
	 * @return the component created
	 */
	public JComponent createConfigurationPanel() {

		JPanel configurationPanel = new JPanel();
		configurationPanel.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.NONE;
		configurationPanel.add(new JLabel("Driver "), constraints);

		constraints.gridx = 1;
		constraints.weightx = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		configurationPanel.add(driversComboBox, constraints);

		constraints.gridx = 2;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.NONE;
		configurationPanel.add(new JLabel("Hôte "), constraints);

		constraints.gridx = 3;
		constraints.weightx = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		configurationPanel.add(hostTextField, constraints);

		constraints.gridx = 4;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.NONE;
		configurationPanel.add(new JLabel("Port "), constraints);

		constraints.gridx = 5;
		constraints.weightx = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		configurationPanel.add(portTextField, constraints);

		return configurationPanel;
	}

	/**
	 * This method create the login panel.
	 * 
	 * @return the component created.
	 */
	public JComponent createLoginPanel() {

		JPanel loginPanel = new JPanel();
		loginPanel.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.NONE;
		constraints.insets = LayoutConfiguration.INSETS;
		loginPanel.add(new JLabel("Nom d'utilisateur"), constraints);

		constraints.gridx = 1;
		constraints.weightx = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		loginPanel.add(userNameTextField, constraints);

		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.NONE;
		loginPanel.add(new JLabel("Mot de passe"), constraints);

		constraints.gridx = 1;
		constraints.weightx = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		loginPanel.add(passwordField, constraints);

		constraints.gridx = 1;
		constraints.gridy = 2;
		constraints.weighty = 1;
		constraints.fill = GridBagConstraints.NONE;
		// WARNING : Don't use NORTH, SOUTH, EAST constants because they can't
		// be localized.
		// Instead, use LINE_END, LINE_START...
		constraints.anchor = GridBagConstraints.LAST_LINE_END;
		JPanel buttonsPanel = new JPanel();
		buttonsPanel.add(cancelButton);
		buttonsPanel.add(loginButton);
		loginPanel.add(buttonsPanel, constraints);

		return loginPanel;

	}

	/**
	 * Gets the controller.
	 * 
	 * @return the controller
	 */
	public DBLoginController getController() {
		return controller;
	}

	/**
	 * Gets the db login.
	 * 
	 * @return the db login
	 */
	public DBLogin getDbLogin() {
		return controller.getDbLogin();
	}

	/**
	 * Gets the user name text field.
	 * 
	 * @return the user name text field
	 */
	public JTextField getUserNameTextField() {
		return userNameTextField;
	}

	/**
	 * Gets the password field.
	 * 
	 * @return the password field
	 */
	public JPasswordField getPasswordField() {
		return passwordField;
	}

	/**
	 * Gets the login button.
	 * 
	 * @return the login button
	 */
	public JButton getLoginButton() {
		return loginButton;
	}

	/**
	 * Gets the drivers combo box.
	 * 
	 * @return the drivers combo box
	 */
	public JComboBox getDriversComboBox() {
		return driversComboBox;
	}

	/**
	 * Gets the host text field.
	 * 
	 * @return the host text field
	 */
	public JTextField getHostTextField() {
		return hostTextField;
	}

	/**
	 * Gets the port text field.
	 * 
	 * @return the port text field
	 */
	public JTextField getPortTextField() {
		return portTextField;
	}

	/**
	 * Gets the cancel button.
	 * 
	 * @return the cancel button
	 */
	public JButton getCancelButton() {
		return cancelButton;
	}

	/**
	 * Update view from model.
	 * 
	 * @param o
	 *            the o
	 * @param arg
	 *            the arg
	 */
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof DBLogin) {
			passwordField.setText(((DBLogin) o).getPassword());
			userNameTextField.setText(((DBLogin) o).getUserName());
			driversComboBox.setSelectedItem(((DBLogin) o).getProtocole());
			hostTextField.setText(((DBLogin) o).getHost());
			portTextField.setText(Integer.valueOf((((DBLogin) o).getPort())).toString());
		}
		// TODO: a compléter
	}
}
